﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PDFWriter
{
    public class PDFDictionary : INode
    {
        public const string DICTIONARY_ELEMENT = "DictionaryElement";
        public const string DICTIONARY = "Dictionary";
        public const string NUMBER = "number";
        public const string NAME = "name";
        public const string VALUE = "value";
        public const string TYPE = "type";

        const string DICTIONARY_START = "<<";
        const string DICTIONARY_END = ">>\n";


        
        private string xmlDictionary = "<Dictionary>"+
        "<DictionaryElement type=\"name\" value=\"ProcSet\">"+
          "<Array>"+
            "<ArrayElement>/PDF</ArrayElement>"+
            "<ArrayElement>/Text</ArrayElement>"+
          "</Array>"+
        "</DictionaryElement>"+
        "<DictionaryElement type=\"name\" value=\"Font\">"+
          "<Dictionary>"+
            "<DictionaryElement type=\"name\" value=\"F1\">"+
              "<ObjRef>4</ObjRef>"+
            "</DictionaryElement>"+
          "</Dictionary>"+
        "</DictionaryElement>"+
      "</Dictionary>";
               
       
        private XmlDocument reader = new XmlDocument();
        
        public PDFDictionary()
        {             
            reader.LoadXml(xmlDictionary);
        }

        public PDFDictionary(string xmlDictionary)
        { 
            this.xmlDictionary = xmlDictionary;
            reader.LoadXml(xmlDictionary);
        }

        public PDFObject GetPDFObject()
        {
            PDFObject pdfObject = new PDFObject() { Text = ProcessXML()};
            return pdfObject;
        }

        public string ProcessXML()
        {
            string resultString = null;
            if (reader.FirstChild.Name == DICTIONARY)
            {
                resultString = DICTIONARY_START;
                foreach(XmlNode node in reader.FirstChild.ChildNodes)
                {
                    if (node.Name == DICTIONARY_ELEMENT)
                    {
                        resultString += GetDictionaryElement(node);
                    }
                }
                resultString += DICTIONARY_END;
            }
            return resultString;
        }

        public string GetDictionaryElement(XmlNode dictionaryElement)
        {
            XmlAttribute attribute=null;
            XmlAttribute attributeValue = null;
            string currentElementString = "";
            if (dictionaryElement.Name == DICTIONARY_ELEMENT)
            {
                 attribute = dictionaryElement.Attributes[TYPE];
                 attributeValue = dictionaryElement.Attributes[VALUE];
                 if (attribute.Value == NAME)
                 {
                     currentElementString += "/" + attributeValue.Value;
                 }
                 else if(attribute.Value == NUMBER)
                 {
                     currentElementString += attributeValue.Value;
                 }
                 if (!dictionaryElement.FirstChild.HasChildNodes)
                 {
                     currentElementString += " " + dictionaryElement.InnerXml+"\n";
                 }
                 else 
                 {
                     currentElementString += " " + Utility.GetPDFString(dictionaryElement.InnerXml);
                 }
            }
            return currentElementString;
        }       

        #region INode Members


        List<PDFObject> INode.GetPDFObjects()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
